﻿using System;
using System.IO;

class Program
{
    static void Main()
    {
        var service = new OrderService();
        InitializeSampleData(service);

        while (true)
        {
            Console.Clear();
            Console.WriteLine("=== 订单管理系统 ===");
            Console.WriteLine("1. 添加订单");
            Console.WriteLine("2. 删除订单");
            Console.WriteLine("3. 修改订单");
            Console.WriteLine("4. 查询订单");
            Console.WriteLine("5. 显示所有订单");
            Console.WriteLine("6. 排序订单");
            Console.WriteLine("7. 导出到XML");
            Console.WriteLine("8. 从XML导入");
            Console.WriteLine("0. 退出");
            Console.Write("请选择: ");

            switch (Console.ReadLine())
            {
                case "1": AddOrder(service); break;
                case "2": DeleteOrder(service); break;
                case "3": UpdateOrder(service); break;
                case "4": QueryOrders(service); break;
                case "5": DisplayAllOrders(service); break;
                case "6": SortOrders(service); break;
                case "7": ExportToXml(service); break;
                case "8": ImportFromXml(service); break;
                case "0": return;
                default: Console.WriteLine("无效输入"); break;
            }
            Console.WriteLine("\n按任意键继续...");
            Console.ReadKey();
        }
    }

    static void InitializeSampleData(OrderService service)
    {
        var customer1 = new Customer("C001", "张三");
        var customer2 = new Customer("C002", "李四");

        var products = new[] {
            new Product("P001", "笔记本电脑", 5999),
            new Product("P002", "手机", 3999),
            new Product("P003", "耳机", 299)
        };

        var order1 = new Order("O001", customer1, DateTime.Now);
        order1.AddDetail(new OrderDetail(products[0], 1));
        order1.AddDetail(new OrderDetail(products[2], 2));

        var order2 = new Order("O002", customer2, DateTime.Now);
        order2.AddDetail(new OrderDetail(products[1], 1));
        order2.AddDetail(new OrderDetail(products[2], 1));

        service.AddOrder(order1);
        service.AddOrder(order2);
    }

    static void AddOrder(OrderService service)
    {
        try
        {
            Console.Write("订单号: ");
            var orderId = Console.ReadLine();

            Console.Write("客户ID: ");
            var customerId = Console.ReadLine();
            Console.Write("客户姓名: ");
            var customerName = Console.ReadLine();

            var order = new Order(orderId, new Customer(customerId, customerName), DateTime.Now);

            while (true)
            {
                Console.Write("添加商品? (y/n): ");
                if (Console.ReadLine().ToLower() != "y") break;

                Console.Write("商品ID: ");
                var productId = Console.ReadLine();
                Console.Write("商品名称: ");
                var productName = Console.ReadLine();
                Console.Write("单价: ");
                var price = decimal.Parse(Console.ReadLine());
                Console.Write("数量: ");
                var quantity = int.Parse(Console.ReadLine());

                order.AddDetail(new OrderDetail(
                    new Product(productId, productName, price), quantity));
            }

            service.AddOrder(order);
            Console.WriteLine("订单添加成功!");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"错误: {ex.Message}");
        }
    }

    static void DeleteOrder(OrderService service)
    {
        Console.Write("输入要删除的订单号: ");
        try
        {
            service.RemoveOrder(Console.ReadLine());
            Console.WriteLine("订单删除成功!");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"错误: {ex.Message}");
        }
    }

    static void UpdateOrder(OrderService service)
    {
        Console.Write("输入要修改的订单号: ");
        var orderId = Console.ReadLine();

        try
        {
            var existing = service.QueryByOrderId(orderId).FirstOrDefault();
            if (existing == null) throw new ArgumentException("订单不存在");

            Console.WriteLine("当前订单信息:");
            Console.WriteLine(existing);

            var updated = new Order(orderId, existing.Customer, DateTime.Now);

            while (true)
            {
                Console.Write("添加商品? (y/n): ");
                if (Console.ReadLine().ToLower() != "y") break;

                Console.Write("商品ID: ");
                var productId = Console.ReadLine();
                Console.Write("商品名称: ");
                var productName = Console.ReadLine();
                Console.Write("单价: ");
                var price = decimal.Parse(Console.ReadLine());
                Console.Write("数量: ");
                var quantity = int.Parse(Console.ReadLine());

                updated.AddDetail(new OrderDetail(
                    new Product(productId, productName, price), quantity));
            }

            service.UpdateOrder(updated);
            Console.WriteLine("订单更新成功!");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"错误: {ex.Message}");
        }
    }

    static void QueryOrders(OrderService service)
    {
        Console.WriteLine("查询方式: 1.按订单号 2.按商品 3.按客户 4.按金额范围");
        Console.Write("请选择: ");

        try
        {
            List<Order> result = null;
            switch (Console.ReadLine())
            {
                case "1":
                    Console.Write("输入订单号: ");
                    result = service.QueryByOrderId(Console.ReadLine());
                    break;
                case "2":
                    Console.Write("输入商品名称: ");
                    result = service.QueryByProductName(Console.ReadLine());
                    break;
                case "3":
                    Console.Write("输入客户ID: ");
                    var customerId = Console.ReadLine();
                    Console.Write("输入客户姓名: ");
                    result = service.QueryByCustomer(new Customer(customerId, Console.ReadLine()));
                    break;
                case "4":
                    Console.Write("最小金额: ");
                    var min = decimal.Parse(Console.ReadLine());
                    Console.Write("最大金额: ");
                    result = service.QueryByAmountRange(min, decimal.Parse(Console.ReadLine()));
                    break;
                default:
                    Console.WriteLine("无效选择");
                    return;
            }

            DisplayOrders(result ?? new List<Order>());
        }
        catch (Exception ex)
        {
            Console.WriteLine($"错误: {ex.Message}");
        }
    }

    static void DisplayAllOrders(OrderService service) =>
        DisplayOrders(service.GetAllOrders());

    static void DisplayOrders(List<Order> orders)
    {
        if (orders.Count == 0)
        {
            Console.WriteLine("没有找到订单");
            return;
        }

        foreach (var order in orders)
        {
            Console.WriteLine(order);
            Console.WriteLine("---------------------");
        }
    }

    static void SortOrders(OrderService service)
    {
        Console.WriteLine("排序方式: 1.按订单号 2.按总金额");
        Console.Write("请选择: ");

        switch (Console.ReadLine())
        {
            case "1": service.SortOrders(); break;
            case "2": service.SortByTotalAmount(); break;
            default: Console.WriteLine("无效选择"); return;
        }

        Console.WriteLine("排序完成");
        DisplayAllOrders(service);
    }

    static void ExportToXml(OrderService service)
    {
        Console.Write("输入导出文件路径: ");
        try
        {
            service.Export(Console.ReadLine());
            Console.WriteLine("导出成功!");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"导出失败: {ex.Message}");
        }
    }

    static void ImportFromXml(OrderService service)
    {
        Console.Write("输入导入文件路径: ");
        try
        {
            service.Import(Console.ReadLine());
            Console.WriteLine("导入成功!");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"导入失败: {ex.Message}");
        }
    }
}